package com.ifinc.common.utils.net;


import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketManager {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) {
        SocketManager manager = new SocketManager();
        manager.doListen();
    }

    public void doListen() {
        ServerSocket server;
        try {
            server = new ServerSocket(8080);
            System.out.println("socket start!!!!!!!!!!!!!!!!!!!");
            while (true) {
                Socket client = server.accept();
                new Thread(new SSocket(client)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //服务器进程
    class SSocket implements Runnable {

        Socket client;

        public SSocket(Socket client) {
            this.client = client;
        }

        public void run() {
            DataInputStream input;
            DataOutputStream output;
            try {
                InputStream inputStream = client.getInputStream();
                input = new DataInputStream(inputStream);
                output = new DataOutputStream(client.getOutputStream());

                byte[] listMsg = new byte[inputStream.available()];
                input.read(listMsg);
                String str = new String(listMsg);
                System.out.println("str = " + str);
//                String str =input.readUTF();
//                System.out.println("str = " + str);
//                output.writeUTF("Recive:  " + listMsg + "    \r\n Thx...");
//                System.out.println("Recive:   " + listMsg);
//                listMsg = input.readUTF();
//                output.writeUTF("Recive Second:  " + listMsg + "    \r\n Thx...");
//                System.out.println("Recive Second:   " + listMsg);
//                output.write("OK!!!".getBytes());
                output.writeUTF("hello");
                output.flush();
//                output.close();
               client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}


